package com.bj58.ecdata.dashboard.utils;

import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class DateUtil {

	/**
	 * 输入日期模式并把date日期对象转换为日期字符串
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getStringDate(Date date, String pattern) {
		try {
			SimpleDateFormat f = new SimpleDateFormat(pattern);
			String strdate = f.format(date);
			return strdate;
		} catch (Exception pe) {
		}
		return null;
	}
	
	/**
	 * 输入日期模式并把date日期对象转换为日期字符串
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getYestoday(String pattern) {
		try {
			SimpleDateFormat f = new SimpleDateFormat(pattern);
			String strdate = f.format(getDateBefore(new Date(),1));
			return strdate;
		} catch (Exception pe) {
		}
		return null;
	}
	
	/**
	 * 将String时间转为Date格式，按照指定格式。
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate, String patter) {
		SimpleDateFormat formatter = new SimpleDateFormat(patter);
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
	
	/**
	 * 输入日期，返回所在月份
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getMonth(String date, String pattern) {
		try {
			Date d = strToDate(date,pattern);
			return getStringDate(d,"yyyyMM");
		} catch (Exception pe) {
			pe.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 输入日期，返回之前i月的月份
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getMonthBefore(String date, String pattern, int i) {
		try {
			Date d = strToDate(date,pattern);
		    Calendar now = Calendar.getInstance();  
		    now.setTime(d);
		    now.add(Calendar.MONTH, -i);
			return getStringDate(now.getTime(),"yyyyMM");
		} catch (Exception pe) {
			pe.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 输入日期，返回之前i月第一天
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String firstDayMonthBefore(String date, String pattern, int i) {
		try {
			Date d = strToDate(date,pattern);
		    Calendar now = Calendar.getInstance();  
		    now.setTime(d);
		    now.add(Calendar.MONTH, -i);
		    now.set(Calendar.DAY_OF_MONTH,1);
			return getStringDate(now.getTime(),"yyyy-MM-dd");
		} catch (Exception pe) {
			pe.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 输入日期，返回之前i月第一天
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String lastDayMonthBefore(String date, String pattern, int i) {
		try {
			Date d = strToDate(date,pattern);
		    Calendar now = Calendar.getInstance();  
		    now.setTime(d);
		    now.add(Calendar.MONTH, -i);
		    now.set(Calendar.DAY_OF_MONTH,now.getActualMaximum(Calendar.DAY_OF_MONTH));
			return getStringDate(now.getTime(),"yyyy-MM-dd");
		} catch (Exception pe) {
			pe.printStackTrace();
		}
		return null;
	}
	/**
	 * 获得到几个月后的时间
	 */  
	public static String getMonthAfter(String nowDate, int month) {  
	       Calendar now = Calendar.getInstance();  
	       Date d = strToDate(nowDate, "yyyy-MM-dd");
	       now.setTime(d);  
	       now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);  
	       return getStringDate(now.getTime(), "yyyy-MM-dd");
	 }  
	   
	/**
	 * 将String时间，按照指定格式，继续转为String
	 */
	public static String strToStr(String strDate, String patter) {
		Date  temp = DateUtil.strToDate(strDate, "yyyy-MM-dd");
		return 	DateUtil.getStringDate(temp, patter);
	}
	
	public static String strToStrBefore(String strDate,String patter,int day){
		Date tmpDate=strToDate(strDate,patter);
		tmpDate=getDateBefore(tmpDate,day);
		return getStringDate(tmpDate,patter);
	}
	
	public static String strToStrAfter(String strDate,String patter,int day){
		Date tmpDate=strToDate(strDate,patter);
		tmpDate=getDateAfter(tmpDate,day);
		return getStringDate(tmpDate,patter);
	}
	
		/** @author zhaoxiang
	    * 获得到几天前的时间
	    * @param d 
	    * @param day 
	    * @return 
	    */ 
	 public static Date getDateBefore(Date d, int day) {  
	       Calendar now = Calendar.getInstance();  
	       now.setTime(d);  
	       now.add(Calendar.DATE, -day);  
	       return now.getTime();  
	   }  
	   
		/** 
	    *  
	    * 获得到几天后的时间
	    * @param d 
	    * @param day 
	    * @return 
	    */  
	   public static Date getDateAfter(Date d, int day) {  
	       Calendar now = Calendar.getInstance();  
	       now.setTime(d);  
	       now.set(Calendar.DATE, now.get(Calendar.DATE) + day);  
	       return now.getTime();  
	   }  
		
		/** 转换时间为字符串格式 自定义格式  */
		public static String getDateStringFromTimestamp(Timestamp tsp,String pattern) {
			Date date = new Date(tsp.getTime());
			SimpleDateFormat f = new SimpleDateFormat(pattern);
			return f.format(date);
		}
		
		public static long getDistDates(Date startDate,Date endDate)    
	    {  
	        long totalDate = 0;  
	        Calendar calendar = Calendar.getInstance();  
	        calendar.setTime(startDate);  
	        long timestart = calendar.getTimeInMillis();  
	        calendar.setTime(endDate);  
	        long timeend = calendar.getTimeInMillis();  
	        totalDate = Math.abs((timeend - timestart))/(1000*60*60*24);  
	        return totalDate;  
	    }
			   
			/**根据起始时间获取其所有日期段
			 * @param startDate
			 * @param endDate
			 * @return
			 */
			public static List<String> getDateList(String startDate,String endDate){
			 	
	        	List<String>  dateList = new ArrayList<String>();
				while(true){
					dateList.add(startDate);
					if(startDate.equals(endDate)){
						break;
					}
					startDate = DateUtil.getStringDate(DateUtil.getDateAfter(DateUtil.strToDate(startDate, "yyyy-MM-dd"), 1), "yyyy-MM-dd");
				}
				
				return dateList;
			}
			
			/**根据起始时间获取其所有日期段 , 返回格式为yyyyMMdd,类似天表名后缀
			 * @param startDate
			 * @param endDate
			 * @return
			 */
			public static List<String> getDayList(String startDate,String endDate){
				startDate = DateUtil.strToStr(startDate, "yyyyMMdd");
				endDate = DateUtil.strToStr(endDate, "yyyyMMdd");
				
	        	List<String>  dateList = new ArrayList<String>();
				while(true){
					dateList.add(startDate);
					if(startDate.equals(endDate)){
						break;
					}
					startDate = DateUtil.strToStrAfter(startDate, "yyyyMMdd" , 1);
				}
				
				return dateList;
			}
			
					
		    /**根据传入的日期获取其月份的第一天日期
		     * @param date
		     * @return
		     */
		    public static String getFirstDay(Date date) {
		        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
		        gcLast.setTime(date);
		        gcLast.set(Calendar.DAY_OF_MONTH, 1);
		        String day_first = df.format(gcLast.getTime());
		        StringBuffer str = new StringBuffer().append(day_first);
		        return str.toString();

		    }
		    
		    /**
		     * 获取当前日期是星期几 (1-7)
		     * 
		     * @param date
		     * @return 当前日期是星期几
		     */
		    public static int getWeekDay(String date, String pattern) {
		    	Date d = strToDate(date,pattern);
		    	Calendar cal = Calendar.getInstance();
//		    	cal.setFirstDayOfWeek(Calendar.MONDAY); setFirstDayOfWeek的方法意思只对WEEK_OF_MONTH与WEEK_OF_YEAR有作用.
		        cal.setTime(d);
		        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		        if(w == 0 ){
		        	w = 7;
		        }
		        return w;
		    }
		    
			/**获取当年年份
			 * @return
			 */
			@SuppressWarnings("static-access")
			public static String getCurYear(){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				String year =String.valueOf(calendar.get(calendar.YEAR));
				return year;
			}
			
			/**获取当前月份
			 * @return
			 */
			public static String getCurMonth(){
				Calendar calendar = Calendar.getInstance();
				Date date = calendar.getTime();
				return getStringDate(date, "yyyyMM");
			}
			
			/**
			 * 输入日期，返回当月天数
			 * @param date
			 * @param pattern
			 * @return
			 */
			public static int getMonthDays(String date, String pattern) {
				try {
					Date d = strToDate(date,pattern);
				    Calendar now = Calendar.getInstance();  
				    now.setTime(d);
					return now.getActualMaximum(Calendar.DAY_OF_MONTH);
				} catch (Exception pe) {
					pe.printStackTrace();
				}
				return 30;
			}
			
			/**
			 * 返回几个月前的时间
			 * @param date
			 * @param pattern
			 * @return
			 */
			public static String getDayBeforeMonth(String nowDate, int month) {
				Calendar now = Calendar.getInstance();  
			       Date d = strToDate(nowDate, "yyyy-MM-dd");
			       now.setTime(d);  
			       now.set(Calendar.MONTH, now.get(Calendar.MONTH) - month+1);  
			       return getStringDate(now.getTime(), "yyyy-MM-dd");
			}
			
}
